from django.shortcuts import render
from rest_framework.viewsets import ReadOnlyModelViewSet
from rest_framework_extensions.cache.mixins import CacheResponseMixin

from .models import Area
from . import serializers
# Create your views here.
#get /areas/
# class AreaView(ListModelMixin,):
#     def list(self):　ListModelMixin和GenericAPIView并用
#         pass
#     def get(self):
#         list()
#     查询Ａｒｅａ数据　复数多个数据对象
#     序列化返回
# class AreaView(ListAPView):
#     ListAPView直接继承ListModelMixin，GenericAPIView
#     实现list方法
#
# #get /areas/<pk>/
# class Sub(RetrieveModelMixin,GenericAPIView)
#     def retrieve(self):
#
#     def get(self):
#         retrieve
# 查询单一的数据库对象 RetrieveModelMixin
# 序列化返回
# RetrieveAPIView 继承RetrieveModelMixin　GenericAPIView

#完成ｌｉｓｔ和ｒｅｔｒｉｅｖｅ操作
# class AreaViewSet(ListModelMixin,RetrieveModelMixin,GenerticViewSet):
class AreasViewSet(CacheResponseMixin,ReadOnlyModelViewSet):
    #分页功能取消
    pagination_class = None

    #重写queryset方法
    def get_queryset(self):
        #action获取请求行为
        if self.action=='list':
            return Area.objects.filter(parent=None)
        else:
            return Area.objects.all()
    def get_serializer_class(self):
        if self.action=='list':
            return serializers.AreaSerializer
        else:
            return serializers.SubAreaSerializer

# /areas/ {'get':'list'} 只返回顶级数据　parent=None
# /areas/<pk> {'get':'retrieve'}
